﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarvellousWorks.PracticalPatternNet8.Concept.Indexer;

public class CustomCollection
{
    private List<string> items;

    public CustomCollection()
    {
        items = new List<string>();
    }

    // 添加元素的方法
    public void Add(string item)
    {
        items.Add(item);
    }

    // 索引器，允许通过索引访问元素
    public string this[int index]
    {
        get
        {
            if (index >= 0 && index < items.Count)
            {
                return items[index];
            }
            else
            {
                throw new IndexOutOfRangeException("索引超出范围");
            }
        }
        set
        {
            if (index >= 0 && index < items.Count)
            {
                items[index] = value;
            }
            else
            {
                throw new IndexOutOfRangeException("索引超出范围");
            }
        }
    }
}